package com.wen.controller;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wen.common.R;
import com.wen.pojo.Merchant;
import com.wen.pojo.Product;
import com.wen.pojo.dto.MerchantDto;
import com.wen.pojo.dto.MerchantStatisticsView;
import com.wen.service.MerchantService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;
import java.sql.Timestamp;
import java.util.Date;
import java.util.List;
import java.util.Map;

@CrossOrigin
@RestController
@RequestMapping("/Merchant")
public class MerchantController {

    @Autowired
    private MerchantService service;


    @PostMapping("sel/{pageNum}/{pageSize}")
    public R sel(@RequestBody MerchantDto dto,@PathVariable Long pageNum,@PathVariable Long pageSize){

        Page<Merchant> merchantPage = service.queryByPage(dto, pageNum, pageSize);

        return R.ok().data("merchants",merchantPage.getRecords()).
                data("total",merchantPage.getTotal()).
                data("current",merchantPage.getCurrent()).
                data("size",merchantPage.getSize());
    }

    @GetMapping("selById")
    public R selById(String id){
        Map<String, Object> columnMap = new HashMap<>();
        columnMap.put("merchant_id", id);
        List<Merchant> merchants = service.listByMap(columnMap);
        return R.ok().data("selById",merchants);
    }

    @PostMapping("up")
    public R up(@RequestBody Merchant merchant){
        System.out.println(merchant);

        UpdateWrapper<Merchant> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("merchant_id",merchant.getMerchantId());
        boolean b = service.update(merchant,updateWrapper);
        if(b)
            return R.ok().message("修改成功");
        return R.ok().message("修改失败");
    }

    /**
     * 订单管理模块（订单查询中的接单商户详情）
     * @param name
     * @return
     */
    @GetMapping("selByName")
    public R selByName(String name, HttpServletRequest request){
        try {

            String keyword = new String(request.getParameter("name").getBytes("ISO-8859-1"), "utf-8");

            System.out.println(keyword);

            Map<String, Object> columnMap = new HashMap<>();
        columnMap.put("shop_name", keyword);
        List<Merchant> merchants = service.listByMap(columnMap);
        return R.ok().data("selByName",merchants);

        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }

        return null;
    }


    @GetMapping("MerchantStatistics/{id}")
    public R aaa(@PathVariable Integer id){
        List<MerchantStatisticsView> merchantStatisticsViews = service.MerchantStatistics(id);
        return R.ok().data("filter",merchantStatisticsViews);
    }



    /**
     * 添加商户
     * @param merchant
     * @return
     */
    @PostMapping("/MerchantAdd")
    public R addProduct(@RequestBody Merchant merchant){
//        merchant.setMerchantId(3);
//        merchant.setBalance(0);//账户余额
//        merchant.setOnOff(0);//门店是否歇业
//        merchant.setCreateDate(new Date());//申请时间
//        merchant.setCheckTag("S001-1");//审核状态
//        merchant.setStatus("C001-1");//是否删除
        int flag = service.AddMerchant(merchant);
        if (flag >0) {
            return R.ok().message("添加成功！");
        }
        return R.error().message("添加失败！");
    }

    @PostMapping("/selectQuery")
    public R selectQuery(@RequestBody MerchantDto dto){

        List<Merchant> merchants = service.queryAll(dto);

        return R.ok().data("merchants",merchants);
    }

    @PostMapping("/selectQueryTwo")
    public R selectQueryTwo(@RequestBody MerchantDto dto){

        Page<Merchant> merchantPage = service.queryAllTwo(dto);

        return R.ok().data("merchants",merchantPage.getRecords()).
                data("total",merchantPage.getTotal()).
                data("current",merchantPage.getCurrent()).
                data("size",merchantPage.getSize());
    }

    @GetMapping("/SelectById/{id}")
    public R getById(@PathVariable String id){
        Merchant selectById = service.getById(id);
        return R.ok().data("selectById",selectById);
    }




}
